package com.xf2048.view;

import com.xf2048.data.GameInformation;
import com.xf2048.global.GameConfig;
import com.xf2048.ui.MainActivity;
import com.zhuifeng.study2048.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class StopMenuDialog extends Dialog {

	private Context context = null;
	private Button backButton = null;
	private Button restartButton = null;
	private Button exitButton = null;
	private Button musicButton = null;
	private Button backMusicButton = null;
	private Button shakeButton = null;
	private GameInformation gameInformation = null;

	public StopMenuDialog(Context context) {
		super(context, R.style.dialog);
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		super.setContentView(R.layout.menu_dialog_view);

		this.init();
	}

	private void init() {
		this.gameInformation = new GameInformation(this.context);
		this.backButton = (Button) findViewById(R.id.menu_dialog_view_back_Button);
		this.restartButton = (Button) findViewById(R.id.menu_dialog_view_restart_Button);
		this.exitButton = (Button) findViewById(R.id.menu_dialog_view_exit_Button);
		this.musicButton = (Button) findViewById(R.id.menu_dialog_view_music_Button);
		this.backMusicButton = (Button) findViewById(R.id.menu_dialog_view_back_music_Button);
		this.shakeButton = (Button) findViewById(R.id.menu_dialog_view_shake_Button);
		this.backButton.setOnClickListener(new MyOnClickListener());
		this.restartButton.setOnClickListener(new MyOnClickListener());
		this.exitButton.setOnClickListener(new MyOnClickListener());
		this.backMusicButton.setOnClickListener(new MyOnClickListener());
		this.musicButton.setOnClickListener(new MyOnClickListener());
		this.shakeButton.setOnClickListener(new MyOnClickListener());

		if (GameConfig.isBackMusic) {
			this.backMusicButton
					.setBackgroundResource(R.drawable.back_music_open);
		} else {
			this.backMusicButton
					.setBackgroundResource(R.drawable.back_music_close);
		}
		if (GameConfig.isSound) {
			this.musicButton.setBackgroundResource(R.drawable.music_open);
		} else {
			this.musicButton.setBackgroundResource(R.drawable.music_close);
		}

		if (GameConfig.isVibrator) {
			this.shakeButton.setBackgroundResource(R.drawable.shake_open);
		} else {
			this.shakeButton.setBackgroundResource(R.drawable.shake_close);
		}
	}

	class MyOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			if (v == backButton) {
				StopMenuDialog.this.dismiss();
			} else if (v == restartButton) {
				((MainActivity) context).restartGame();
				StopMenuDialog.this.dismiss();
			} else if (v == exitButton) {
				// Intent intent = new Intent(context, WelcomeActivity.class);
				// context.startActivity(intent);
				((MainActivity) context).setBackground();
				StopMenuDialog.this.dismiss();
			} else if (v == backMusicButton) {
				gameInformation.setBackMusic(!GameConfig.isBackMusic);
				GameConfig.isBackMusic = !GameConfig.isBackMusic;
				if (GameConfig.isBackMusic) {
					backMusicButton
							.setBackgroundResource(R.drawable.back_music_open);
				} else {
					backMusicButton
							.setBackgroundResource(R.drawable.back_music_close);
				}
				((MainActivity) context).setBackMusic();
			} else if (v == musicButton) {
				gameInformation.setSound(!GameConfig.isSound);
				GameConfig.isSound = !GameConfig.isSound;
				if (GameConfig.isSound) {
					musicButton.setBackgroundResource(R.drawable.music_open);
				} else {
					musicButton.setBackgroundResource(R.drawable.music_close);
				}
			} else if (v == shakeButton) {
				gameInformation.setShake(!GameConfig.isVibrator);
				GameConfig.isVibrator = !GameConfig.isVibrator;
				if (GameConfig.isVibrator) {
					shakeButton.setBackgroundResource(R.drawable.shake_open);
				} else {
					shakeButton.setBackgroundResource(R.drawable.shake_close);
				}
			}
		}
	}

}
